package gfs;

import javax.swing.JOptionPane;

/**
 * This class uses a bulk of JOptionPane modified to more specialized purposes.
 * This is ideally a simplier implementation of their methods and make the class
 * slightly easier to use and work better for the purposes of this project. All 
 * methods are static and you cannot create of instance of this class. You merely
 * need to call one of the static methods to use only of the different display 
 * dialogs. 
 * 
 * @author  Mark Vismonte
 * @author  Joel Jauregui
 * @version 2.0, 07/06/08
 * @see	    JOptionPane
 * @since   1.6
 */

public class SwingIO
    {
    /** Used to indicate that a message dialog is to be used. */
    public static final int MSG_DIALOG = 1;
    
    /** Used to indicate that an input dialog is to be used. */
    public static final int INPUT_DIALOG = 2;
    
    /** Used to indicate that a confirm dialog is to be used. */
    public static final int CONFIRM_DIALOG = 3;
    
    //
    // Option types
    //
    /** 
     * Type meaning Look and Feel should not supply any options -- only
     * use the options from the <code>JOptionPane</code>.
     */
    /** Type used for <code>MSG_DIALOG</code>. */
    public static final int YES_NO = JOptionPane.YES_NO_OPTION;
    
    /** Type used for <code>MSG_DIALOG</code>. */
    public static final int OK_CANCEL = JOptionPane.OK_CANCEL_OPTION;
    
    /** Type used for <code>MSG_DIALOG</code>. */
    public static final int YES_NO_CANCEL = JOptionPane.YES_NO_CANCEL_OPTION;
    
    //
    // Return values.
    //
    /** Return value from class method if YES is chosen. */
    public static final int OPT_YES = JOptionPane.OK_OPTION;
    
    /** Return value from class method if NO is chosen. */
    public static final int OPT_NO = JOptionPane.NO_OPTION;
    
    /** Return value from class method if CANCEL is chosen. */
    public static final int OPT_CANCEL = JOptionPane.CANCEL_OPTION;
    
    /** Return value from class method if user closes window without selecting
     * anything, more than likely this should be treated as either a
     * <code>CANCEL_OPTION</code> or <code>NO_OPTION</code>. */
    public static final int         CLOSED_OPTION = -1;
    
    //
    // Message types. Used by the JOptionPane UI to determine what icon to display,
    // and possibly what behavior to give based on the type.
    //
    /** Used for error messages. */
    public static final int  ERROR_MSG = 0;
    /** Used for information messages. */
    public static final int  INFO_MSG = 1;
    /** Used for warning messages. */
    public static final int  WARN_MSG = 2;
    /** Used for questions. */
    public static final int  QUESTION_MSG = 3;
    /** No icon is used. */
    public static final int   PLAIN_MSG = -1;
    
    private SwingIO()
        {
        throw new UnsupportedOperationException("Not supported yet.");
        }
    
    /**
     * Auxillary method to get a number from the user 
     * 
     * @param input
     *      The <code>String</code> to be the title of the window to be displayed
     * to the user
     *      
     * @return
     *      The number that the user has input
     */
    private static int findInt(String input)
    	{
    	String number = "";
    	for (boolean zTrue = true; zTrue; zTrue = !checkInt(number))
            number = getString(input);
	return Integer.parseInt(number);
    	}
    
    /**
     * Checks if a given String is a number or not.
     * 
     * @param number
     *      The String to be checked if it is a valid number or not.
     * @return
     *      True if number is a number else false.
     */
    private static boolean checkInt(String number)
        //returns true if it is an int && returns false if it is not
    	{
        if(number == null)
            return false;
    	char[] array = number.toCharArray();
    	if (array.length == 0)
            return false;
        for(int i = 0; i < array.length; i++)
            if(!Character.isDigit(array[i]))
                return false;
    	return true;
    	}
    
    /**
     * Get a number from the user with a window with a default message
     * 
     * @return
     *      The integer value input by the user
     */
    public static int getInt()
        {
        return findInt("Please input an integer");
        }
    
    /**
     * Gets a number from the user with a window with a specificed message
     * 
     * @param input
     *      The <code>String</code> name of the window to be displayed to user
     * @return
     *      The integer value input by the user
     */
    public static int getInt(String input)
        {
        return findInt(input);
        }
    
    /**
     * Auxillary method that is used to display all dialog types to user.
     * 
     * @param messege
     *      The message to display to the user
     * @param windowName
     *      The name of the window to display to the user
     * @param optionType
     *      Used only for panelType equals to <code>CONFIRM_DIALOG</code> that 
     * specifies the number of options avalible in the dialog:
     * <code>YES_NO</code> or <code>YES_NO_CANCEL</code> or <code>OK_CANCEL</cod>
     * @param messageType the type of message to be displayed:
     *                  <code>ERROR_MESSAGE</code>,
     *			<code>INFORMATION_MESSAGE</code>,
     *			<code>WARNING_MESSAGE</code>,
     *                  <code>QUESTION_MESSAGE</code>,
     *			or <code>PLAIN_MESSAGE</code>
     * @param panelType
     *      The type of dialog panel to be used being either: 
     *                  <code>MSG_DIALOG</code>,
     *                  <code>INPUT_DIALOG</code>, or
     *                  <code>CONFIRM_DIALOG</code>
     * @return 
     *      If the panelType is <code>INPUT_DIALOG</code> a string object 
     * is returned, in the form of a general object, of the input from the user.
     * If the panelType is <code>CONFIRM_DIALOG</code> an Integer is returned, in
     * the form of a general object, indicating the option selected by the user.
     */
    public static Object systemOut(String messege, String windowName, 
            int optionType, int messageType, int panelType)
        {
        Integer val = null;
        String str = null;
        switch(panelType)
            {
            case MSG_DIALOG:
            JOptionPane.showMessageDialog(null, messege, windowName, 
                    messageType);
            break;
            case INPUT_DIALOG:
            str = JOptionPane.showInputDialog(null, messege, windowName,
                    messageType);
            break;
            case CONFIRM_DIALOG:
            val = JOptionPane.showConfirmDialog(null, messege, windowName,
                    optionType, messageType);
            break;
            default:
                throw new IllegalArgumentException("WTF MATE");
            }
        return val == null ? str : val;
        }
    
    /**
     * Displays a error message to the user
     *  
     * @param messege
     *      The error message to display to the user
     */
    public static void errorMsg(String messege)
        {    
        systemOut(messege, "Error!", 0, JOptionPane.ERROR_MESSAGE, MSG_DIALOG);
        }
    
    /**
     * Displays an informational message to the user
     * 
     * @param messege
     *      The string to display to the user
     * @param windowName
     *      The name of the window to display to the user
     */
    public static void infoMsg(String messege, String windowName)
        {
        systemOut(messege, windowName, 0, JOptionPane.INFORMATION_MESSAGE,
                MSG_DIALOG);
        }
    
    /**
     * Displays an question input dialog to the user with a message
     * 
     * @param message
     *      The message to display to the user
     * @param windowName
     *      The name of the window to display to the user
     */
    public static String inputMsg(String message, String windowName)
        {
        Object o = 
                systemOut(message, windowName, 0, QUESTION_MSG, INPUT_DIALOG);
        return ((String)o).toString();
        }
    
    /**
     * A standard yes or no confirm dialog to display to the user
     * 
     * @param message
     *      The message to display to the user
     * @param windowName
     *      The name of the window to display to the user
     */
    public static int confirmMsg(String message, String windowName)
        {
        Object o = 
                systemOut(message, windowName, YES_NO, QUESTION_MSG,
                CONFIRM_DIALOG);
        return ((Integer)o).intValue();
        }
    
    /**
     * Gets a string from the user
     * 
     * @param input
     *      The name of the window to display to the user
     * @return
     *      The string input by the user
     */
    public static String getString(String input)
    	{
    	return JOptionPane.showInputDialog(input);
    	}
    
} // class StrategoIO
